#include "ClientManager.h"

int ClientManager::_idCounter = 1;

ClientManager::ClientManager()
{
}

QList<ClientInfo> ClientManager::users()
{
	return _users;
}

void ClientManager::addUser(ClientThread* thread)
{
	ClientInfo info;
	info.socketId = thread->getThreadId();
	info.user.setId(_idCounter++);
	info.user.setState(USER_IDLE);
	_users.append(info);
}

void ClientManager::removeUser(ClientThread* thread)
{
	for(int i=0;i<_users.size();i++){
		if(_users.at(i).socketId == thread->getThreadId()){
			_users[i].socketId = -1;
		}
	}
}

void ClientManager::updateName(ClientThread* thread, QString name)
{
	foreach (ClientInfo info, _users) {
		if(info.socketId == thread->getThreadId()){
			info.user.setName(name);
		}
	}
}

void ClientManager::updateStatus(ClientThread* thread, UserState state)
{
	foreach (ClientInfo info, _users) {
		if(info.socketId == thread->getThreadId()){
			info.user.setState(state);
		}
	}
}
